package sca;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

@EnableFeignClients
@SpringBootApplication
public class ScaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScaConsumerApplication.class,args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalancedRestTemplate(){
        return new RestTemplate();
    }

    @Service
    public class ConsumerService{
        @SentinelResource("doGetResource")
        public String doGetResource(){
            return "get My Toto";
        }
    }


    @RestController
    public class ConsumerController{

        @Value("${server.port}")
        private String serverPort;

        @Autowired
        private RestTemplate restTemplate;

        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @Autowired
        private RestTemplate loadBalancedRestTemplate;

        @Autowired
        private ConsumerService consumerService;

        @GetMapping("/consumer/doFindById")
        @SentinelResource("findById")
        public String doFindById(@RequestParam("id") Integer id){
            return "the resource is: "+id;
        }

        private AtomicLong atomicLong = new AtomicLong(1);

        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho01() throws InterruptedException {
            long andIncrement = atomicLong.getAndIncrement();
            System.out.println(andIncrement);
            if (andIncrement%2 == 0){
//                Thread.sleep(200);
                throw new RuntimeException("熔断异常测试");
            }
//            consumerService.doGetResource();
            String url = "http://localhost:8081/provider/echo/"+serverPort;
            System.out.println("request url:"+url);
            return restTemplate.getForObject(url, String.class);
        }

        @GetMapping("/consumer/doRestEcho02")
        public String doRestEcho02(){
            ServiceInstance serviceInstance =
                    loadBalancerClient.choose("sca-provider");
            String ip = serviceInstance.getHost();
            int port = serviceInstance.getPort();
            String url = "http://"+ip+":"+port+"/provider/echo/"+serverPort;
            String url2 = String.format("http://%s:%s/provider/echo/%s",
                    serviceInstance.getHost(),
                    serviceInstance.getPort(),
                    serverPort);
            System.out.println("request url:"+url);
            return restTemplate.getForObject(url, String.class);
        }


        @GetMapping("/consumer/doRestEcho03")
        public String doRestEcho03(){
            String url = String.format("http://%s/provider/echo/%s", "sca-provider",serverPort);
            return loadBalancedRestTemplate.getForObject(url, String.class);
        }

    }
}



